package com.anyikang.volunteer.sos.volunteer.app.userInfo;

import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.MyApplication;
import com.anyikang.volunteer.sos.volunteer.app.bean.User;
import com.anyikang.volunteer.sos.volunteer.app.view.SelectPicPopupWindow;
import com.anyikang.volunteer.sos.volunteer.utils.FileUtil;

import java.io.File;

import static com.anyikang.volunteer.sos.volunteer.R.id.avatarImg;

public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {

    RelativeLayout rl_avatar;
    private SelectPicPopupWindow menuWindow;
    private static final int REQUESTCODE_PICK = 0;
    private static final int REQUESTCODE_CAMERA = 1;
    private static final String IMAGE_FILE_NAME = "avatarImage.jpg";
    private static final int REQUESTCODE_CUTTING = 2;
    private ImageView imvAvatar;
    private String headPicFilePath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();
    }

    @Override
    protected void onResume() {
        initData();
        super.onResume();
    }

    /**
     * 初始化视图控件
     */
    private void initView() {
        rl_avatar = (RelativeLayout) this.findViewById(R.id.rl_avatar);
        rl_avatar.setOnClickListener(this);
        imvAvatar = (ImageView) this.findViewById(R.id.imvAvatar);
    }

    /**
     *
     */
    public void initData()
    {
        //读取用户信息，并赋值给各个控件
        User user = new User(this);
        String headPicPath = user.getHeadPicPath();

        if(!TextUtils.isEmpty(headPicPath))
        {
            Bitmap bmp = BitmapFactory.decodeFile(headPicPath);
            imvAvatar.setImageBitmap(bmp);
        }
        return;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.rl_avatar:

                menuWindow = new SelectPicPopupWindow(this, itemsOnClick);
                menuWindow.showAtLocation(findViewById(R.id.activity_main),
                        Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
        }
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.takePhotoBtn:
                    Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    takeIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            Uri.fromFile(new File(Environment.getExternalStorageDirectory(), IMAGE_FILE_NAME)));
                    startActivityForResult(takeIntent, REQUESTCODE_CAMERA);
                    break;
                case R.id.pickPhotoBtn:
                    Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
                    pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(pickIntent, REQUESTCODE_PICK);
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            case REQUESTCODE_PICK:  //从相册里选择返回的图片
                try {
                    startPhotoZoom(data.getData());
                } catch (NullPointerException e) {
                    e.printStackTrace();// ?????????????
                }
                break;
            case REQUESTCODE_CAMERA:// 拍照返回的图片
                File temp = new File(Environment.getExternalStorageDirectory() + "/" + IMAGE_FILE_NAME);
                startPhotoZoom(Uri.fromFile(temp));
                break;
            case REQUESTCODE_CUTTING://
                if (data != null) {
                    setPicToView(data);
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void startPhotoZoom(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, REQUESTCODE_CUTTING);
    }


    private void setPicToView(Intent picdata) {
        Bundle extras = picdata.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            Drawable drawable = new BitmapDrawable(null, photo);
            headPicFilePath = FileUtil.saveFile(this, "temphead.jpg", photo);
            MyApplication.userPicFilePath = headPicFilePath;
            User user = new User();
            ContentValues userValues = new ContentValues();
            userValues.put("headPicPath",headPicFilePath);
            user.saveUserInfo(UserInfoActivity.this,userValues);
            imvAvatar.setImageDrawable(drawable);

            //上传图片，暂时略掉
     /*   gxw-s    pd = ProgressDialog.show(mContext, null, "????????????????...");
            new Thread(uploadImageRunnable).start();
            gxw-e*/
        }
    }

}
